package org.dfl.plugin.views.actions;

import org.dfl.core.data.History;
//import org.dfl.core.data.Token;
//import org.dfl.core.model.Place;
import org.dfl.plugin.DflPlugin;
import org.dfl.plugin.views.debug.HistoriesTreeView;
import org.dfl.plugin.views.debug.TokensTreeView;
import org.dfl.plugin.views.debug.model.HistoriesTreeViewRoot;
import org.dfl.plugin.views.debug.model.HistoryTreeElem;
//import org.dfl.plugin.views.debug.model.TreeElemFactory;
import org.eclipse.jface.action.IAction;
import org.eclipse.ui.IViewActionDelegate;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.actions.ActionDelegate;

/**
 * action that provide adding new history. 
 * It's used in a HistoriesTreeView (see mapping in plugin.xml file)
 * @author Piotr Wlodarczyk
 *
 */
public class AddHistoryAction extends ActionDelegate implements  IViewActionDelegate {
	/* (non-Javadoc)
	 * @see org.eclipse.ui.actions.ActionDelegate#run(org.eclipse.jface.action.IAction)
	 */
	public void run(IAction action) {
			if(view!=null && view.getCommonViewer().getInput()!=null && view.getPlace()!=null){
				HistoriesTreeViewRoot root = (HistoriesTreeViewRoot) view.getCommonViewer().getInput();
				
//				History hist = new History();
//				Place place = view.getPlace();
//				Object data = TreeElemFactory.getEmptyInstance(null, place.getTypeContainer().getType()).getValue();
//				Token token = new Token(data,hist);
				
				HistoriesTreeView historiesTreeView = HistoriesTreeView .getView();
				TokensTreeView tokensTreeView = TokensTreeView.getView();
				// place listeners have to be removed before 
				if(historiesTreeView!=null) historiesTreeView.stopListenPlace();
				if(tokensTreeView!=null) tokensTreeView.stopListenPlace();
				synchronized (DflPlugin.class) {
//					place.addToken(token);
					root.addChild(new HistoryTreeElem(root,view.getPlace(),new History(),null));
				}
				// restore place listeners
				if(historiesTreeView!=null) historiesTreeView.startListenPlace();
				if(tokensTreeView!=null) tokensTreeView.startListenPlace();
				
				view.getCommonViewer().refresh();
			}			
	}

	HistoriesTreeView view;
	public void init(IViewPart view) {
		if(view instanceof HistoriesTreeView)
			this.view = (HistoriesTreeView) view;
	}
}
